﻿Imports System.IO
Imports System.Threading
Imports System.Xml.Serialization
Imports System.Text.RegularExpressions

Public Class Tools

#Region "Read/Write Settings"

    ''' <summary>
    ''' Diese Funktion serialisiert ein Objekt und speichert es als XML-Format Datei
    ''' </summary>
    ''' <param name="Obj">Das zu serialisierende Objekt</param>
    ''' <param name="FileName">Die XML Datei in der das Ergebnis gespeichert werden soll</param>
    Public Sub XMLSerialize(ByVal Obj As Object, ByVal FileName As String)
        Dim XML As New XmlSerializer(Obj.GetType)
        Dim FS As New FileStream(FileName, FileMode.Create)
        XML.Serialize(FS, Obj)
        FS.Close()
    End Sub

    ''' <summary>
    ''' Diese Funktion deserialisiert eine XML Datei und gibt das Ergebnis in ein Objekt zurück
    ''' </summary>
    ''' <param name="FileName">Die XML Datei aus der gelesen werden soll</param>
    Public Function XMLDeSerialize(ByVal Obj As Object, ByVal FileName As String) As Object
        Dim XML As New XmlSerializer(Obj.GetType)
        Dim FS As New FileStream(FileName, FileMode.Open)
        Dim RetObj As Object = XML.Deserialize(FS)
        FS.Close()
        Return RetObj
    End Function


#End Region

    ''' <summary>
    ''' Funktion zum prüfen ob es sich um einen validen MD5-Hash handelt
    ''' </summary>
    ''' <param name="s"></param>
    ''' <return></return>
    ''' <remarks></remarks>

    Public Function isValidMD5(s As String) As Boolean

        Dim _regex As New Regex("[0-9a-f]{32}")

        Dim _match As Match = _regex.Match(s)

        If _match.Success Then
            Return True
        Else
            Return False
        End If

    End Function

    ''' <summary>
    ''' Funktion zum Parsen des TransfersMode
    ''' </summary>
    ''' <param name="_mode"></param>
    ''' <return></return>
    ''' <remarks></remarks>

    Public Function ParseMode(ByVal _mode As String) As FTP.FTP.TransferMode

        If _mode.ToLower = "active" Then
            Return FTP.FTP.TransferMode.Active
        Else
            Return FTP.FTP.TransferMode.Passive
        End If

    End Function

    ''' <summary>
    ''' Funktion zum Parsen des TransferTypes
    ''' </summary>

    Public Function ParseTransferMode(ByVal _mode As String) As FTP.FTP.TransferMode

        If _mode.ToLower = "binary" Then
            Return FTP.FTP.TransferType.Binary
        Else
            Return FTP.FTP.TransferType.Ascii
        End If

    End Function

    ''' <summary>
    ''' Funktion zum Parsen des Encodings
    ''' </summary>

    Public Function ParseEncoding(ByVal _mode As String) As Enums.CharacterEncoding


        If _mode.ToLower = "standard" Then
            Return Enums.CharacterEncoding.Standard
        End If

        If _mode.ToLower = "utf-8" Then
            Return Enums.CharacterEncoding.UTF8
        End If

        If _mode.ToLower = "utf-7" Then
            Return Enums.CharacterEncoding.UTF7
        End If

        If _mode.ToLower = "ascii" Then
            Return Enums.CharacterEncoding.ASCII
        End If

        Return Enums.CharacterEncoding.Standard

    End Function

    Public Function ParseSecurityProtocol(ByVal _index As Integer) As FTP.FTP.FtpSecurityProtocol

        Select Case _index

            Case 0

                Return FTP.FTP.FtpSecurityProtocol.None

            Case 1

                Return FTP.FTP.FtpSecurityProtocol.Ssl2Explicit

            Case 2

                Return FTP.FTP.FtpSecurityProtocol.Ssl2Implicit

            Case 3

                Return FTP.FTP.FtpSecurityProtocol.Ssl3Explicit

            Case 4

                Return FTP.FTP.FtpSecurityProtocol.Ssl3Implicit

            Case 5

                Return FTP.FTP.FtpSecurityProtocol.Tls1Explicit

            Case 6

                Return FTP.FTP.FtpSecurityProtocol.Tls1Implicit

            Case 7

                Return FTP.FTP.FtpSecurityProtocol.Tls1OrSsl3Explicit

            Case 8

                Return FTP.FTP.FtpSecurityProtocol.Tls1OrSsl3Implicit

            Case Else

                Return FTP.FTP.FtpSecurityProtocol.None

        End Select

    End Function

    Public Function ConvertEncodingEnumToEncoding(ByVal _encoding As Enums.CharacterEncoding) As System.Text.Encoding

        Select Case _encoding

            Case Enums.CharacterEncoding.Standard

                Return System.Text.Encoding.Default

            Case Enums.CharacterEncoding.UTF8

                Return System.Text.Encoding.UTF8

            Case Enums.CharacterEncoding.UTF7

                Return System.Text.Encoding.UTF7

            Case Enums.CharacterEncoding.ASCII

                Return System.Text.Encoding.ASCII

            Case Else

                Return System.Text.Encoding.Default

        End Select

    End Function

    ''' <summary>
    ''' Löscht die SFDL Queue
    ''' </summary>
    Public Sub ClearSFDLQueue()

        frm_main.lv_sfdl_container.Items.Clear()
        frm_main.lbl_directory_root.Text = String.Empty

    End Sub

    ''' <summary>
    ''' Funktion zum Parsen eines FTP Connection Strings
    ''' </summary>

    Public Function ParseFTPConnectionString(ByVal _ftp_string As String) As ConnectionInfo

        Dim _ftp_server As New ConnectionInfo

        If String.IsNullOrEmpty(_ftp_string) = False And _ftp_string.StartsWith("ftp://") Then

            _ftp_server.GetSetHost = _ftp_string.Replace("ftp://", "")

            _ftp_server.GetSetPort = 21

            If _ftp_server.GetSetHost.Contains("@") Then
                _ftp_server.GetSetAuthRequired = True
                _ftp_server.GetSetUsername = _ftp_server.GetSetHost.Split(":")(0).ToString
                _ftp_server.GetSetPassword = _ftp_server.GetSetHost.Split(":")(1).ToString
                _ftp_server.GetSetPassword = _ftp_server.GetSetPassword.Remove(_ftp_server.GetSetPassword.IndexOf("@"))
                _ftp_server.GetSetHost = _ftp_server.GetSetHost.Split("@")(1).ToString
            End If

            If _ftp_server.GetSetHost.Contains("/") Then
                _ftp_server.GetSetPath = _ftp_server.GetSetHost.Remove(0, _ftp_server.GetSetHost.IndexOf("/"))
                _ftp_server.GetSetHost = _ftp_server.GetSetHost.Remove(_ftp_server.GetSetHost.IndexOf("/"))
            End If

            If _ftp_server.GetSetHost.Contains("\") Then
                _ftp_server.GetSetPath = _ftp_server.GetSetHost.Remove(0, _ftp_server.GetSetHost.IndexOf("\"))
                _ftp_server.GetSetHost = _ftp_server.GetSetHost.Remove(_ftp_server.GetSetHost.IndexOf("\"))
            End If

            If _ftp_server.GetSetHost.Contains(":") Then
                _ftp_server.GetSetPort = Integer.Parse(_ftp_server.GetSetHost.Split(":")(1).ToString)
                _ftp_server.GetSetHost = _ftp_server.GetSetHost.Remove(_ftp_server.GetSetHost.IndexOf(":"))
            End If

        End If

        Return _ftp_server

    End Function

End Class
